package codeTop;

public class q61 {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null) {
            return null;
        }
        ListNode pre = new ListNode();
        pre.next = head;
        ListNode tail = pre;
        int len = 0;
        while (tail.next != null) {
            tail = tail.next;
            len++;
        }
        k = k%len;
        if (k == 0) return head;
        ListNode p = pre;
        for (int i = 0; i <len-k ; i++) {
            p = p.next;
        }
        ListNode res = p.next;
        p.next = null;
        tail.next = pre.next;
        return res;
    }
}
